﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.Windows;
using Silvermoon;
using Silvermoon.UI;
using Silvermoon.Images;
using Silvermoon.OpenGL;
using Silvermoon.Shapes;

namespace Passwords.Controls
{
    public class SearchPicker : Panel
    {
        public static Command ClearCommand = new Command();
        public static Command FilterCommand = new Command();

        const float HiddenAngle = 70f;

        private Label title;
        private TextBox textBox;
        private Button clearButton;
        private RotateTransform pickerRotation;
        private bool isShowing = false;

        public static PropertyAccessor<SearchPicker, float> PickerRotationProperty = PropertyAccessor.Register<SearchPicker, float>("PickerRotation",
            (c) => c.PickerRotation,
            (c, v) => c.PickerRotation = v);

        static SearchPicker()
        {
            VisualStateManager manager = new VisualStateManager(
                new VisualState("Show",
                    new FloatTransition { Property = PickerRotationProperty, From = HiddenAngle, To = 0, Duration = 500, Tracking = TrackMode.Keep },
                    new IntTransition { Property = Control.OpacityProperty, From = 0, To = 255, Tracking = TrackMode.Keep, Duration = 100 }
                  ),
                new VisualState("Hide",
                    new FloatTransition { Property = PickerRotationProperty, To = HiddenAngle, Duration = 500, Tracking = TrackMode.Keep },
                    new IntTransition { Property = Control.OpacityProperty, To = 0, Tracking = TrackMode.Keep, Duration = 500 },
                    new BoolTransition { Property = Control.VisibleProperty, To = false, Tracking = TrackMode.Keep, Duration = 500 }
                    ));

            VisualStateManager.RegisterClassManager(typeof(SearchPicker), manager);
        }

        public SearchPicker()
            : base()
        {
            Visible = false;
            Opacity = 0;
            pickerRotation = new RotateTransform { Axis = RotateAxis.XAxis, Angle = HiddenAngle, OrigY = 0 };
            Transformation = pickerRotation;
            Background = Color.Black;
            title = new Label { Text = "Filter", Margin = new Margin(8, 8, 8, null), Height = 32, LineAlignment = Alignment.Near };
            textBox = new TextBox { Margin = new Margin(8, null, 8, 5), Height = 64 };
            textBox.TextChanged += new EventHandler(OnTextChanged);
            clearButton = new Button
            {
                Content = SysImages.Cancel,
                TextColor = PhoneColors.Black,
                Color = Color.Transparent,
                Margin = new Margin(null, null, 8, 5),
                Size = new Size(64, 64),
                Command = ClearCommand,
                IsFocusable = false,
                StyleName = "ClearFilterButton",
                Chrome = Shape.Empty
            };
            Controls.Add(title, textBox, clearButton);
            Height = 80 + 48;
        }

        void OnTextChanged(object sender, EventArgs e)
        {
            //RaiseCommand(FilterCommand);
            if (TextChanged != null) TextChanged(this, EventArgs.Empty);
        }

        public event EventHandler TextChanged;

        protected override void OnCommand(CommandEventArgs args)
        {
            if (args.Command == ClearCommand)
            {
                textBox.Text = String.Empty;
                textBox.Focus();
                Window.IsKeyboardVisible = true;
                args.Handled = true;
            }
            base.OnCommand(args);
        }

        protected float PickerRotation
        {
            get { return pickerRotation.Angle; }
            set
            {
                pickerRotation.Angle = value;
                Invalidate(InvalidationFlag.Transformation);
            }
        }

        public bool IsShowing
        {
            get { return isShowing; }
            set
            {
                if (isShowing != value)
                {
                    isShowing = value;
                    OnShowChanged();
                }
            }
        }

        protected virtual void OnShowChanged()
        {
            if (isShowing)
            {
                textBox.Text = string.Empty;
                Visible = true;
                Window.IsKeyboardVisible = true;
                textBox.Focus();
            }
            else
            {
                textBox.Text = string.Empty;
            }
            SetState(isShowing ? "Show" : "Hide");
            Invalidate(InvalidationFlag.Appearance);
        }


        public string Filter
        {
            get { return textBox.Text; }
            set
            {
                textBox.Text = value;
            }
        }


    }
}
